var app = getApp(), actmodel = {
    serverlimitcars: {},
    temdata: {},
    addlimit: function(t, e, i) {
        var a = t.data.carnum, o = wx.getStorageSync("limitcars") || [], l = !1;
        o && (o.forEach(function(t, a) {
            i.flash_id == t.flash_id && t.id == i.id && t.attr == e && (l = !0, wx.showToast({
                title: "该商品已经在购物车中",
                icon: "none"
            }));
        }), l) || (o.push({
            id: i.id,
            num: t.data.num,
            price: i.attrs[t.data.attr].sale,
            attr: e,
            cid: 1,
            expires: i.expires,
            flash_id: i.flash_id
        }), wx.setStorage({
            key: "limitcars",
            data: o,
            success: function() {
                t.setData({
                    carnum: a + 1
                }), wx.showToast({
                    title: "已加入购物车"
                });
            }
        }));
    },
    dealerrolimitcars: function(t) {
        t && 0 < t.length || wx.showModal({
            title: "其它错误",
            content: "系统错误请联系管理员"
        });
        var i = wx.getStorageSync("limitcars") || [], o = [];
        t.forEach(function(t, a) {
            var e = i.length;
            for (a = 0; a < e; a++) if (o.push(t.name + "【" + t.attr + "】"), t.keyval == i[a].flash_id + "_" + i[a].id && t.attr == i[a].attr) {
                i.splice(a, 1);
                break;
            }
        }), wx.showModal({
            title: "产品过期产品",
            content: o.join(",") + "\r\n以上活动产品过期，删除并跳转购物车重新下单",
            showCancel: !1,
            success: function(t) {
                wx.redirectTo({
                    url: "/xc_xinguwu/pages/cart/cart"
                });
            }
        });
    },
    loadcartdata: function(d) {
        var m = wx.getStorageSync("cars") || [], n = wx.getStorageSync("limitcars") || [], e = [], i = {};
        0 < m.length || 0 < n.length ? (m.forEach(function(t, a) {
            e.push(t.id);
        }), 0 < n.length && n.forEach(function(t, a) {
            i[t.flash_id + "_" + t.id] = 1;
        }), wx.showLoading({
            title: "加载中"
        }), console.log("发送数据"), console.log(e), console.log(Object.keys(i)), app.util.request({
            url: "entry/wxapp/goods",
            showLoading: !1,
            method: "POST",
            data: {
                op: "cars",
                ids: e,
                flash_idandids: Object.keys(i)
            },
            success: function(t) {
                console.log("服务器返回"), console.log(t);
                var a = {}, e = t.data;
                if (e.data.list) {
                    a = {};
                    var i = JSON.parse(JSON.stringify(e.data.list));
                    console.log(i);
                    for (var o = [], l = 0, r = m.length; l < r; l++) if (app.look.istrue(i[m[l].id]) && i[m[l].id].attrs && i[m[l].id].attrs[m[l].attr]) {
                        var s = {
                            status: 1,
                            num: m[l].num,
                            stock: i[m[l].id].attrs[m[l].attr].stock,
                            attr: m[l].attr,
                            price: i[m[l].id].attrs[m[l].attr].price,
                            id: m[l].id,
                            bimg: i[m[l].id].bimg,
                            name: i[m[l].id].name,
                            weight: i[m[l].id].weight,
                            vprice: i[m[l].id].vprice,
                            selected: !!m[l].selected
                        };
                        o.push(s), 0 != parseInt(s.stock) ? s.num > s.num.stock && (s.num = s.num.stock) : m[l].del = -1;
                    } else m[l].del = -1;
                    console.log("cart1"), a.hasList = !0, a.carts = o;
                    for (l = m.length - 1; 0 <= l; l--) -1 == m[l].del && m.splice(l, 1);
                    wx.setStorage({
                        key: "cars",
                        data: m
                    }), actmodel.getTotalPrice(d);
                } else wx.setStorage({
                    key: "cars",
                    data: []
                }), e.data.limitcars ? a.hasList = !0 : a.hasList = !1;
                if (e.data.limitcars) {
                    actmodel.temdata = JSON.parse(JSON.stringify(e.data.limitcars)), actmodel.inittemdata(), 
                    actmodel.serverlimitcars = e.data.limitcars;
                    var c = actmodel.cartlist(n, e.data.limitcars);
                    a.limitcarsdata = c;
                }
                console.log("update"), console.log(a), d.setData(a), actmodel.getTotalPrice(d);
            },
            complete: function(t) {
                console.log("一定返回"), console.log(t);
            },
            error: function(t) {
                console.log("error"), console.log(t);
            }
        })) : d.setData({
            hasList: !1
        });
    },
    inittemdata: function() {
        var t, a;
        for (var e in actmodel.temdata) actmodel.temdata[e].temnum = 0, t = actmodel.temdata[e].limitnum - actmodel.temdata[e].allnums, 
        a = actmodel.temdata[e].limitsale - actmodel.temdata[e].mybuynum, actmodel.temdata[e].userlimit = a < t ? a : t;
    },
    vildadd: function() {},
    vildstock: function(t, a, e) {
        return actmodel.temdata[t] ? actmodel.temdata[t].userlimit <= 0 || actmodel.temdata[t].temnum >= actmodel.temdata[t].userlimit ? 0 : actmodel.temdata[t].temnum + a <= actmodel.temdata[t].userlimit ? (e && (actmodel.temdata[t].temnum += a), 
        a) : (e && (actmodel.temdata[t].temnum = actmodel.temdata[t].userlimit), actmodel.temdata[t].userlimit - actmodel.temdata[t].temnum) : 0;
    },
    cartlist: function(o, l) {
        console.log("限时活动");
        var r = [], s = "";
        o.forEach(function(t, a) {
            if (s = t.flash_id + "_" + t.id, l[s] && l[s].attrs && l[s].attrs[t.attr]) {
                var e = actmodel.vildstock(s, t.num, !0);
                if (0 == e) o[a].del = -1; else {
                    var i = {};
                    i.id = t.id, i.flash_id = t.flash_id, i.attr = t.attr, i.bimg = l[s].bimg, i.name = l[s].name, 
                    i.oprice = l[s].oprice, i.num = e, i.expires = l[s].expires, i.date_end = l[s].date_end, 
                    i.weight = l[s].weight, i.price = l[s].attrs[t.attr].sale, i.selected = !!t.selected, 
                    r.push(i);
                }
            } else o[a].del = -1;
        });
        for (var t = o.length - 1; 0 <= t; t--) -1 == o[t].del && o.splice(t, 1);
        return wx.setStorage({
            key: "limitcars",
            data: o
        }), r;
    },
    getTotalPrice: function(t) {
        for (var a = getApp(), e = t.data.carts, i = 0, o = 0, l = 0; l < e.length; l++) if (e[l].selected) {
            var r = e[l].price;
            1 == a.globalData.webset.vip && a.globalData.userInfo.member && (r = 0 < e[l].vprice ? a.look.toDecimal(e[l].vprice * r / 100) : a.look.toDecimal(a.globalData.userInfo.member.discount * r / 100)), 
            i += e[l].num * r, o += e[l].num;
        }
        var s = {};
        s.carts = e;
        var c = t.data.limitcarsdata;
        c && 0 < c.length && c.forEach(function(t, a) {
            t.selected && (i += t.num * t.price, o += t.num);
        }), s.totalPrice = a.look.toDecimal(i), s.totalNum = o, t.setData(s);
    }
};

module.exports.carthepler = actmodel;